import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { CreatePageViewDto, NameValueOfInt32, PageViewDto, PageViewServiceProxy, UpdatePageViewDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { finalize } from 'rxjs/operators';
import { AppModalComponentBase } from '@shared/common/app-modal-component-base';

@Component({
    selector: 'create-or-edit-pageview-modal',
    templateUrl: './create-or-edit-pageview-modal.component.html'
})
export class CreateOrEditPageViewModalComponent extends AppModalComponentBase {

  
    @ViewChild('modal', { static: true }) modal: ModalDirective;
    @Output() callback: EventEmitter<any> = new EventEmitter<any>();

    saving = false;
    loading = false;

    title: string = '';

    frontendPageId: string = null;
    deviceTypes: NameValueOfInt32[] = [];
    data: PageViewDto;


    constructor(
        injector: Injector,
        private _pageViewServiceProxy: PageViewServiceProxy
    ) {
        super(injector);
        this.data = new PageViewDto();
    }



    show(id: string, frontendPageId: string) {
        this.frontendPageId = frontendPageId;
        this.modal.show();

        this.loading = true;
        this.data = new PageViewDto();
        this._pageViewServiceProxy.getForEdit(id)
        .pipe(finalize(() => this.loading = false))
        .subscribe(result=> {
            this.deviceTypes = result.deviceTypes;
            if (result.pageView) {
                this.data = result.pageView;
                this.title = result.pageView.name;
                this.title += '(' + this.data.id + ')';
            } else {
                this.title = this.l("CreateNew");
            }

            if (!this.data.pcOrMobile) {
                this.data.pcOrMobile = this.deviceTypes[0].value;
            }
        });
    }

    close(): void {
        this.modal.hide();
    }

    save() {
        this.data.frontEndPageId = this.frontendPageId;

        if (this.data.id) {
            this.saving = true;
            let input : UpdatePageViewDto = new UpdatePageViewDto({
                id: this.data.id,
                name: this.data.name,
                isActive: this.data.isActive,
                pageSize: this.data.pageSize,
                bodyView: this.data.bodyView,
                pcOrMobile: this.data.pcOrMobile,
                frontEndPageId: this.data.frontEndPageId
            });
            this._pageViewServiceProxy.update(input)
            .pipe(finalize(() => this.saving = false))
            .subscribe(result=> {
                this.notify.success(this.l('SavedSuccessfully'));
                this.callback.emit();
                this.close();
            });
        }else{
            this.saving = true;
            let input : CreatePageViewDto= new CreatePageViewDto({
                name: this.data.name,
                isActive: this.data.isActive,
                pageSize: this.data.pageSize,
                bodyView: this.data.bodyView,
                pcOrMobile: this.data.pcOrMobile,
                frontEndPageId: this.data.frontEndPageId
            });
           
            this._pageViewServiceProxy.create(input)
            .pipe(finalize(() => this.saving = false))
            .subscribe(result=> {
                this.notify.success(this.l('SavedSuccessfully'));
                this.callback.emit();
                this.close();
            });
        }
    }
}
